package com.outsider.util.music;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.outsider.util.OkHttp3;
import com.outsider.util.cache.TimeCache;



public class Netease {
	private static final String SEARCH_URL = "http://music.163.com/api/search/pc";
	//默认半小时清理缓存
    private static final TimeCache<Integer, String> cache=new TimeCache<>(1800000);
    private static final HashMap<String, String> headers = new HashMap<>();
    static{
        headers.put("Referer","http://music.163.com/");
        headers.put("appver","1.5.2");
    }
	public static List<Music> search(String name) {
        HashMap<String, String> params = new HashMap<>();
        List<Music> result=new ArrayList<>();
        params.put("s", name);
        params.put("limit", "10");
        params.put("type", "1");
        params.put("offset", "0");
        OkHttp3 okHttp3=OkHttp3.getInstance();
        
        JSONObject json=JSONObject.parseObject(okHttp3.post(SEARCH_URL, headers,params));
        if (json.getInteger("code")!=200) return null;
        else{
        	JSONArray songs=json.getJSONObject("result").getJSONArray("songs");
        	for (int i=0;i<songs.size();i++){
        		JSONObject song=songs.getJSONObject(i);
        		if (song.getInteger("status")==-1) continue;
        		Music music=convertMusic(song);
        		result.add(music);
        		cache.put(song.getInteger("id"), song.getString("mp3Url"));
        	}
        }
        
        return result;
	}


	private static Music convertMusic(JSONObject song) {
		Music music=new Music();
		music.setID(song.getString("id"));
		music.setName(song.getString("name"));
		
		JSONArray singers=song.getJSONArray("artists");
		StringBuilder singer=new StringBuilder();
		singer.append(singers.getJSONObject(0).getString("name")+" ");
		music.setSinger(singer.toString().trim());
		music.setSingerID(singers.getJSONObject(0).getString("id"));
		
		JSONObject album=song.getJSONObject("album");
		music.setAlbum(album.getString("name"));
		music.setAlbumID(album.getString("id"));
		
		music.setPlaytime(song.getInteger("duration")/1000);
		music.setSource(Music.SOURCE_NETEASE);
		
		return music;
	}
	
	
	public static String getURL(String ID) {
		Integer id=Integer.parseInt(ID);
		String url=cache.get(id);
		//缓存过期 正常情况不发生
		if (url==null){
			OkHttp3 okHttp3=OkHttp3.getInstance();
			String detailUrl="http://music.163.com/api/song/detail/?id="+ID+"&ids=%5B"+ID+"%5D";
			JSONObject json=JSONObject.parseObject(okHttp3.get(detailUrl));
			return json.getJSONArray("songs").getJSONObject(0).getString("mp3Url");
		}
		return url;
	}
	
	private static final String SINGER_URL = "http://music.163.com/api/artist/";
	public static JSONObject getListBySinger(String singerID) {
		JSONObject result=new JSONObject();
		OkHttp3 okHttp3=OkHttp3.getInstance();
		JSONObject json=JSONObject.parseObject(okHttp3.getWithHeader(SINGER_URL+singerID, headers));
		if (json.getInteger("code")!=200) return null;
		result.put("cover_img_url", json.getJSONObject("artist").getString("picUrl"));
		result.put("title",json.getJSONObject("artist").getString("name"));
		result.put("id", Music.SOURCE_NETEASE+"_singer_"+singerID);
		JSONArray hotsongs=json.getJSONArray("hotSongs");
		List<Music> hotsongList=new ArrayList<>();
		for (int i=0;i<hotsongs.size();i++){
    		JSONObject song=hotsongs.getJSONObject(i);
    		if (song.getInteger("status")==-1) continue;
    		Music music=convertMusic(song);
    		hotsongList.add(music);
    		cache.put(song.getInteger("id"), song.getString("mp3Url"));
		}
		result.put("tracks", hotsongList);
		result.put("is_mine", 0);
		return result;
	}

	private static final String ALBUM_URL = "http://music.163.com/api/album/";
	public static JSONObject getListByAlbum(String albumID) {
		JSONObject result=new JSONObject();
		OkHttp3 okHttp3=OkHttp3.getInstance();
		JSONObject json=JSONObject.parseObject(okHttp3.getWithHeader(ALBUM_URL+albumID, headers));
		if (json.getInteger("code")!=200) return null;
		result.put("cover_img_url", json.getJSONObject("album").getString("picUrl"));
		result.put("title",json.getJSONObject("album").getString("name"));
		result.put("id", Music.SOURCE_NETEASE+"_album_"+albumID);
		JSONArray hotsongs=json.getJSONObject("album").getJSONArray("songs");
		List<Music> songList=new ArrayList<>();
		for (int i=0;i<hotsongs.size();i++){
    		JSONObject song=hotsongs.getJSONObject(i);
    		if (song.getInteger("status")==-1) continue;
    		Music music=convertMusic(song);
    		songList.add(music);
    		cache.put(song.getInteger("id"), song.getString("mp3Url"));
		}
		result.put("tracks", songList);
		result.put("is_mine", 0);
		return result;
	}	
	
	
	public static void main(String args[]) {
		List<Music> list=Netease.search("锦鲤抄");
		System.out.println(getURL(list.get(0).getID()));
		
	}
}
